﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using Recon.Portal.Business;
using Recon.Portal.DataObjects;


namespace Recon.Portal.WebUI
{
    public partial class SearchCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                control.Value = Request.QueryString["textbox"].ToString();
                path.Value = Request.QueryString["PageRequest"].ToString();
                BindGrid();
                txtCustomerId.Focus();

            }
        }


        #region User Defined Function

        public void BindGrid()
        {
            CustomerEntity custEntity = new CustomerEntity();
            List<Customer> lstCustomer = null;
            try
            {
                if (txtCustomerId.Text.Trim() == "")
                {
                    lstCustomer = custEntity.GetCustomerDetailsByCustomerId(control.Value.Trim());

                    if (lstCustomer == null)
                    {
                        lblCustomercnt.Text = "No Records Found";
                    }
                    else
                    {
                        gvCustomer.DataSource = lstCustomer;
                        gvCustomer.DataBind();
                        lblCustomercnt.Text = ((List<Customer>)gvCustomer.DataSource).Count.ToString() + " records found.";
                    }
                }
                else
                {
                    lstCustomer = custEntity.GetCustomerDetailsByCustomerId(txtCustomerId.Text.Trim());

                    if (lstCustomer == null)
                    {
                        lblCustomercnt.Text = "No Records Found";
                    }
                    else
                    {
                        gvCustomer.DataSource = lstCustomer;
                        gvCustomer.DataBind();
                        lblCustomercnt.Text = ((List<Customer>)gvCustomer.DataSource).Count.ToString() + " records found.";
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
        #region Gridview Events
        
        protected void btnsearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        

        

        //protected void gvCustomer_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    gvCustomer.PageIndex = e.NewPageIndex;
        //    BindGrid();
        //}

        protected void gvCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            string customerid;
            customerid = ((LinkButton)gvCustomer.SelectedRow.FindControl("lbtnCustomerid")).Text;
            string strScript = "<script>";
            strScript += "window.opener.document.all('ctl00$BodyHolder$txtCustomerId').value = '" + customerid + "';";
            strScript += "self.close();</script>";
            Response.Write(strScript);
        }

        protected void gvCustomer_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        #endregion

    }
}